﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Threading;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TGD610
    {
        #region ==EXTPROD===========
        public static readonly DependencyProperty EXTPRODProperty = DependencyProperty.Register("EXTPROD",
        typeof(string), typeof(TGD610),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXTPRODChanged));
        public string EXTPROD
        {
            get
            {
                return (string)GetValue(EXTPRODProperty);
            }
            set
            {
                SetValue(EXTPRODProperty, value);
                OnEXTPRODChg(value);
            }
        }
        private static void OnEXTPRODChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGD610;
            if (src != null)
            {
                src.EXTPROD = (string)e.NewValue;
            }
        }
        public virtual void OnEXTPRODChg(string value)
        {
            if (dwMaster == null)
                return;

        }
        #endregion
        public TGD610()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGAAUF();
            this.DataContext = MV;
            InitializeComponent();

        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwGD == null)
            {
                CART = "";
                KEY = "";
                AUF = "";
            }
            else
            {
                CART = dwGD["GAART"].ToString() + dwGD["GAVAR"].ToString() + dwGD["GAFARBE"].ToString();
                KEY = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString() + "VK ";
                AUF = "K" + dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString();
            }
            ((VGAAUF)MV).RESETPROD();
            if (dwMU == null)
            {
                cMU.Visibility = Visibility.Collapsed;
            }
            else
            {
                if (cMU.TBMain.Rows.Count > 1)
                {
                    cMU.Visibility = Visibility.Visible;
                }
            }
        }

        public override void OrderCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                PM["RUNID"] = RUNID;
                //根据不同点的原料类型，需求创建不同的订单
                //在创建时，决定使用现有库存还是新订单 
                if (dwGD == null)
                    return;
                PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GASA"].ToString() + dwGD["GAPOS"].ToString(); ;
                //销售单规则已经创建好，这里捕捉实际值，并保存规则
                if (dwRule == null)
                    return;
                if (dwRule["P3"].ToString() != "E" & dwRule["P3"].ToString() != "F" & dwRule["P3"].ToString() != "X")
                {
                    return;
                }
                //PM["KUNDNR"] = dwRule["P5"].ToString();
                double GSFACH = Convert.ToDouble((((VGAAUF)MV).P2));
                MessageInfo msg = new MessageInfo();
                #region===产生ABKOPF======
                PM["NEWABK"] = "F";
                if (dwMU == null)
                {
                    PM["NEWABK"] = "T";
                    PM["KRS"] = "FB";
                    if (PM.ContainsKey("LFD"))
                    {
                        PM.Remove("LFD");
                    }
                    PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + "-" + Helpers.Helper.Right(dwGD["GAAUFLFD"].ToString(), 4) + "-" + dwGD["GAAUFPOS"].ToString();
                    PM["RUNID"] = this.RUNID;
                    PM["AUFART"] = "GP";
                    TMV.VABK.NEWABK(PM);
                }
                if (dwABK == null)
                    return;
                #endregion
                if (!dwABK["ABKAUFKRS"].ToString().StartsWith("F"))
                {
                    if (MessageBox.Show("单号不是FB类型，再次执行", "请确认", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                    {
                        return;
                    }
                    else
                    {
                        PM["NEWABK"] = "T";
                        PM["KRS"] = "FB";
                        if (PM.ContainsKey("LFD"))
                        {
                            PM.Remove("LFD");
                        }
                        PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + "-" + Helpers.Helper.Right(dwGD["GAAUFLFD"].ToString(), 4) + "-" + dwGD["GAAUFPOS"].ToString();
                        PM["RUNID"] = this.RUNID;
                        PM["AUFART"] = "GP";
                        TMV.VABK.NEWABK(PM);
                    }
                    //
                }
               
                #region===产生首个计划规则，传递参数======
                msg = new MessageInfo();
                msg.AUFTYP = "K";
                msg.WAKZ = "G";
                msg.ART = dwGD["GAART"].ToString();
                msg.VAR = dwGD["GAVAR"].ToString();
                msg.FARBE = dwGD["GAFARBE"].ToString();
                msg.KRS = dwGD["GAAUFKRS"].ToString();
                msg.LFD = dwGD["GAAUFLFD"].ToString();
                msg.POS = dwGD["GAAUFPOS"].ToString();
                msg.P1 = dwGD["GAPOS"].ToString();
                msg.P2 = dwGD["GASA"].ToString();
                msg.Where = app.User;
                double QTYMAT = 0, QTYEND = 0, F02 = 0;
                QTYEND = Convert.ToDouble(dwRule["VKPQTY"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
                QTYMAT = QTYEND - F02;//需求的成品数量
                if (QTYMAT == 0)
                {
                    throw new Exception("订单数量为0,无需创建");
                }
                msg.Sender = this.RUNID;

                PM["CART"] = dwGD["GAART"].ToString() + dwGD["GAVAR"].ToString() + dwGD["GAFARBE"].ToString();
                PM["VKPAUF"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString() + "K";
                PM["VKAUF"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString() + "K";
                if (GSFACH > 1)
                {
                    msg.MSGTYPE = "TWABPNEW";
                    msg.ACTION = "TWABPNEW";
                    PM["ARTSTAGE"] = "T";
                    PM["VKPQTY"] = dwRule["VKPQTY"].ToString();
                    PM["DELDATE"] = dwRule["DELDATE"].ToString();
                    PM["YARNTYP"] = dwRule["P1"].ToString();
                    PM["GSFACH"] = dwRule["P2"].ToString();
                    PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "030" + "BX ";
                    PM["COLOR"] = "BX ";
                    PM["AUFTYP"] = "V";
                    PM["QTYEND"] = QTYMAT;
                    VICC_PRULES.Instance.NEWPLAN(PM);
                }
                else
                {
                    msg.MSGTYPE = "XSABPNEW";
                    msg.ACTION = "XSABPNEW";
                    PM["ARTSTAGE"] = "X";
                    PM["DELDATE"] = dwRule["DELDATE"].ToString();
                    PM["VKPQTY"] = dwRule["VKPQTY"].ToString();
                    PM["YARNTYP"] = dwRule["P1"].ToString();
                    PM["GSFACH"] = dwRule["P2"].ToString();
                    PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "020" + "XS ";
                    PM["COLOR"] = "XS ";
                    PM["AUFTYP"] = "V";
                    PM["QTYEND"] = QTYMAT;
                    VICC_PRULES.Instance.NEWPLAN(PM);
                }
                #endregion
                PM["CUSER"] = app.User;
                PM["EVENTID"] = "MYGRABFB01";
                PM["dwInput"] = dwGD;
                PM["KRS"] = dwGD["GAAUFKRS"];
                PM["LFD"] = dwGD["GAAUFLFD"];
                PM["POS"] = dwGD["GAAUFPOS"];
                PM["MSG"] = new MessageInfo();
                PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("创建复板单");
                VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
                msg.P3 = (string)PM["NEWABK"];
                msg.ORDER = (string)PM["ORDERNUM"];
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void CHKGD(MessageInfo msg)
        {
            IsChg = false;
            while(msg.ORDER.Substring(0,11)!=KEY.Substring(0, 11))
            {
                Thread.Sleep(1000);
            }
            if (dwGD == null)
                return;
            if (CART != msg.ART + Helper.Right("   " + msg.VAR, 3) + msg.FARBE)
            {
                IsChg = true;
            }
            if (dwRule != null)
            { 
                if (Helper.RoundX(Convert.ToDouble(dwRule["VKPQTY"].ToString()), 100) != Helper.RoundX(Convert.ToDouble(msg.QTY), 100))
                {
                    IsChg = true;
                }
            }
            else
            {
                if (Helper.RoundX(GAKG1 / 100, 100) != Helper.RoundX(Convert.ToDouble(msg.QTY), 100))
                {
                    IsChg = true;
                }
            }
            if (dwRule == null)
            {
                CHGGD(msg);
            }
            if (!IsChg)
            {

                return;
            }
            else
            {
                LastMsg = msg;
                MessageBox.Show("数量有变更，请点击确认并更新计划");
                return;
            }
        }

        private void CommmandChg(object sender, RoutedEventArgs e)
        {
            if (LastMsg == null)
                return;
            CHGGD(LastMsg);
        }

        ITXABPLANData ABP;

        private void RemoveCommand(object sender, RoutedEventArgs e)
        {
            if (txtDel.Text == "N")
            {
                MessageBox.Show("请手工把标志设置为Y");
                return;
            }
            if (dwGD == null)
                return;
            if (ABP == null)
                ABP = new ITXABPLANData();
            string krs = dwGD["GAAUFKRS"].ToString();
            string lfd = dwGD["GAAUFLFD"].ToString();
            string pos = dwGD["GAAUFPOS"].ToString();

            ABP.DeleteAV95(krs, lfd, pos,"FB");
            DataRow dwData = VICC_QUERYTB.Instance.TBMain.NewRow();
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = krs;
            dwData["VKPAUFLFD"] = lfd;
            dwData["VKPAUFPOS"] = pos;
            dwData["C20"] = 0;
            INTEXDBC.UpdateVKPOS(dwData, app.company, 8);
            dwData["GAMANDANT"] = app.Mandant;
            dwData["GAAUFKRS"] = krs;
            dwData["GAAUFLFD"] = lfd;
            dwData["GAAUFPOS"] = pos;
            dwData["C20"] = 0;
            INTEXDBC.UpdateGAAUF(dwData, 6);
            PM["CUSER"] = app.User;
            PM["EVENTID"] = "MYGRABFB08";
            PM["dwInput"] = dwGD;
            PM["KRS"] = dwGD["GAAUFKRS"];
            PM["LFD"] = dwGD["GAAUFLFD"];
            PM["POS"] = dwGD["GAAUFPOS"];
            PM["MSG"] = new MessageInfo();
            PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("复板单删除");
            VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
            txtDel.Text = "N";
            PM["VKPAUF"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString();
            PM["RUNID"] = this.RUNID;
            PM["VKPEXART"] = " ".PadRight(20);
            VVKPOS.Instance.VKPCHK(PM);
        }

        public void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnCompensation_Click(sender, e);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                MessageBox.Show("分多批需要重新查询销售单");
            }

        }
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
        }
        
        private void btnCompensation_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IsFP = true;
                if (SQTY <= 0)
                {
                    return;
                }
                if (dwGD == null)
                    return;
                DataRow _dSGD = TBMain.NewRow();
                _dSGD.ItemArray = dwGD.ItemArray;
                _dSGD["GAKG1"] = SQTY;
                PM["RUNID"] = RUNID;
                //根据不同点的原料类型，需求创建不同的订单
                //在创建时，决定使用现有库存还是新订单 
                if (dwGD == null)
                    return;
                PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GASA"].ToString() + dwGD["GAPOS"].ToString(); ;
                //销售单规则已经创建好，这里捕捉实际值，并保存规则
                if (dwRule == null)
                    return;
                if (dwRule["P3"].ToString() != "E" & dwRule["P3"].ToString() != "F" & dwRule["P3"].ToString() != "X")
                {
                    return;
                }
                double GSFACH = Convert.ToDouble((((VGAAUF)MV).P2));
                MessageInfo msg = new MessageInfo();
                #region===产生ABKOPF======

                PM["NEWABK"] = "T";
                /*  GSFACH = System.Convert.ToDouble(dwRule["P2"].ToString());
                  if (GSFACH > 1)
                  {
                      PM["KRS"] = "B" + MV.year.Substring(3, 1); ;
                  }
                  else
                  {
                      PM["KRS"] = "X" + MV.year.Substring(3, 1); ;
                  }*/
                PM["KRS"] = "FB";
                PM["ORDERNUM"] = dwGD["GAAUFKRS"].ToString() + "-" + Helpers.Helper.Right(dwGD["GAAUFLFD"].ToString(), 4) + "-" + dwGD["GAAUFPOS"].ToString();
                PM["RUNID"] = this.RUNID;
                PM["AUFART"] = "GP";
                VABKOPF.Instance.NEWABK(PM);

                if (dwABK == null)
                    return;
                #endregion

                #region===产生首个计划规则，传递参数======
                msg = new MessageInfo();
                msg.AUFTYP = "K";
                msg.ART = dwGD["GAART"].ToString();
                msg.VAR = dwGD["GAVAR"].ToString();
                msg.FARBE = dwGD["GAFARBE"].ToString();
                msg.KRS = dwGD["GAAUFKRS"].ToString();
                msg.LFD = dwGD["GAAUFLFD"].ToString();
                msg.POS = dwGD["GAAUFPOS"].ToString();
                msg.P1 = dwGD["GAPOS"].ToString();
                msg.P2 = dwGD["GASA"].ToString();
                msg.Where = app.User;
                double QTYMAT = 0, QTYEND = 0, F02 = 0;
                QTYEND = 0.01 * SQTY;
                F02 = 0;
                QTYMAT = QTYEND;//需求的成品数量
                if (QTYMAT == 0)
                {
                    throw new Exception("订单数量为0,无需创建");
                }
                msg.Sender = this.RUNID;
                PM["CART"] = dwGD["GAART"].ToString() + dwGD["GAVAR"].ToString() + dwGD["GAFARBE"].ToString();
                PM["VKPAUF"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString();
                PM["VKAUF"] = dwGD["GAAUFKRS"].ToString() + dwGD["GAAUFLFD"].ToString() + dwGD["GAAUFPOS"].ToString() + dwGD["GAPOS"].ToString() + "K";

                PM["VKPQTY"] = QTYEND;
                if (GSFACH > 1)
                {
                    msg.MSGTYPE = "TWABPNEW";
                    msg.ACTION = "TWABPNEW";
                    PM["ARTSTAGE"] = "T";
                    PM["DELDATE"] = dwRule["DELDATE"].ToString();
                    PM["YARNTYP"] = dwRule["P1"].ToString();
                    PM["GSFACH"] = dwRule["P2"].ToString();
                    PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "030" + "BX ";
                    PM["COLOR"] = "BX ";
                    PM["AUFTYP"] = "V";
                    PM["QTYEND"] = QTYMAT;
                    VICC_PRULES.Instance.NEWPLAN(PM);
                }
                else
                {
                    msg.MSGTYPE = "XSABPNEW";
                    msg.ACTION = "XSABPNEW";
                    PM["ARTSTAGE"] = "X";
                    PM["DELDATE"] = dwRule["DELDATE"].ToString();
                    PM["YARNTYP"] = dwRule["P1"].ToString();
                    PM["GSFACH"] = dwRule["P2"].ToString();
                    PM["ORDERNUM"] = dwABK["ABKAUFKRS"].ToString() + dwABK["ABKAUFLFD"].ToString() + "020" + "XS ";
                    PM["COLOR"] = "XS ";
                    PM["AUFTYP"] = "V";
                    PM["QTYEND"] = QTYMAT;
                    VICC_PRULES.Instance.NEWPLAN(PM);
                }
                #endregion

                msg.P3 = (string)PM["NEWABK"];
                msg.ORDER = (string)PM["ORDERNUM"];
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
                IsFP = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SQTY = -1;
            }
        }
    }
}
